<?php

namespace app\base\controller\admin;

use app\base\model\logs\MemberBalanceLogsModel;

use app\base\model\logs\MemberCouponLogsModel;

use app\base\model\logs\MemberIntegralLogsModel;

use app\base\model\member\MemberModel;

use app\BaseController;

use app\common\logic\KefuLogic;

use think\facade\Cache;



class Home extends BaseController{

    public function index()

    {

        //商铺信息

        $this->request->shop->append(['expire_time_format','is_expire']);

        $shop=[

            'type'=>$this->request->shop->type,

            'expire_time_format'=>$this->request->shop->expire_time_format,

            'is_expire'=>$this->request->shop->is_expire,

        ];



        //客服

        $kefu = KefuLogic::getKefu($this->request->shopId);



        //会员余额储值

        if(!Cache::get('base_shop_'.$this->request->shopId.'_home_balance_recharge_data')){

            $total_recharge_balance = $total_recharge_give_balance = 0;

            $today_recharge_balance = $today_recharge_give_balance = 0;

            $this_month_recharge_balance = $this_month_recharge_give_balance=0;

            $total_recharge_balance_data = MemberBalanceLogsModel::where([

                ['shop_id','=',$this->request->shopId],

                ['type','=',1]

            ])->field('shop_id,sum(balance) as balance,sum(give_balance) as give_balance')

                ->group('shop_id')->select()->toArray();



            $today_recharge_balance_data = MemberBalanceLogsModel::where([

                ['shop_id','=',$this->request->shopId],

                ['type','=',1],

            ])->whereDay('add_time')

                ->field('shop_id,sum(balance) as balance,sum(give_balance) as give_balance')

                ->group('shop_id')->select()->toArray();



            $this_month_recharge_balance_data = MemberBalanceLogsModel::where([

                ['shop_id','=',$this->request->shopId],

                ['type','=',1],

            ])->whereMonth('add_time')

                ->field('shop_id,sum(balance) as balance,sum(give_balance) as give_balance')

                ->group('shop_id')->select()->toArray();



            if(!empty($total_recharge_balance_data)){

                //累计

                $total_recharge_balance=$total_recharge_balance_data[0]['balance'];

                //累计赠送的余额

                $total_recharge_give_balance=$total_recharge_balance_data[0]['give_balance'];

            }

            if(!empty($today_recharge_balance_data)){

                //今日金额

                $today_recharge_balance=$today_recharge_balance_data[0]['balance'];

                //今日赠送的金额

                $today_recharge_give_balance=$today_recharge_balance_data[0]['give_balance'];

            }

            if(!empty($this_month_recharge_balance_data)){

                //本月金额

                $this_month_recharge_balance=$this_month_recharge_balance_data[0]['balance'];

                //本月赠送的金额

                $this_month_recharge_give_balance=$this_month_recharge_balance_data[0]['give_balance'];

            }

            $balance_recharge_data=[

                'total_recharge_balance'=>$total_recharge_balance,

                'total_recharge_give_balance'=>$total_recharge_give_balance,

                'today_recharge_balance'=>$today_recharge_balance,

                'today_recharge_give_balance'=>$today_recharge_give_balance,

                'this_month_recharge_balance'=>$this_month_recharge_balance,

                'this_month_recharge_give_balance'=>$this_month_recharge_give_balance,

            ];

            Cache::set('base_shop_'.$this->request->shopId.'_home_balance_recharge_data',$balance_recharge_data,600);

        }else{

            $balance_recharge_data=Cache::get('base_shop_'.$this->request->shopId.'_home_balance_recharge_data');

        }





        if(!Cache::get('base_shop_'.$this->request->shopId.'_home_balance_consume_data')){

            //会员余额核销

            $total_consume_balance = MemberBalanceLogsModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type','=',-1]

                ])->sum('balance') ?? 0;



            $today_consume_balance = MemberBalanceLogsModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type','=',-1],

                ])->whereDay('add_time')->sum('balance') ?? 0;



            $this_month_consume_balance = MemberBalanceLogsModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type','=',-1],

                ])->whereMonth('add_time')->sum('balance') ?? 0;

            $balance_consume_data=[

                'total_consume_balance'=>$total_consume_balance,

                'today_consume_balance'=>$today_consume_balance,

                'this_month_consume_balance'=>$this_month_consume_balance,

            ];

            Cache::set('base_shop_'.$this->request->shopId.'_home_balance_consume_data',$balance_consume_data,600);

        }else{

            $balance_consume_data=Cache::get('base_shop_'.$this->request->shopId.'_home_balance_consume_data');

        }





        if(!Cache::get('base_shop_'.$this->request->shopId.'_home_coupon_consume_data')){

            //优惠券使用张数

            $total_consume_coupon = MemberCouponLogsModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type','=',-1]

                ])->count() ?? 0;



            $today_consume_coupon = MemberCouponLogsModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type','=',-1],

                ])->whereDay('add_time')->count() ?? 0;



            $this_month_consume_coupon = MemberCouponLogsModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['type','=',-1],

                ])->whereMonth('add_time')->count() ?? 0;

            $coupon_consume_data=[

                'total_consume_coupon'=>$total_consume_coupon,

                'today_consume_coupon'=>$today_consume_coupon,

                'this_month_consume_coupon'=>$this_month_consume_coupon,

            ];

            Cache::set('base_shop_'.$this->request->shopId.'_home_coupon_consume_data',$coupon_consume_data,600);

        }else{

            $coupon_consume_data=Cache::get('base_shop_'.$this->request->shopId.'_home_coupon_consume_data');

        }





        //充值会员排行榜

        $balance_time_type=$this->request->param('balance_time_type/d',1);

        $member_balance_logs_model = new MemberBalanceLogsModel();

        if($balance_time_type == 1) $member_balance_logs_model = $member_balance_logs_model->whereMonth('add_time');

        if($balance_time_type == 2) $member_balance_logs_model = $member_balance_logs_model->whereYear('add_time');

        $balance_rank_data = $member_balance_logs_model->where([

            ['shop_id','=',$this->request->shopId],

            ['type','=',1],

        ])->field('member_id,sum(balance) as balance,sum(give_balance) as give_balance')

            ->group('member_id')->order(['balance'=>'desc','give_balance'=>'desc'])

            ->limit(5)->select()->toArray();

        if(!empty($balance_rank_data)){

            $memberIds =  [];

            foreach($balance_rank_data as $val){

                $memberIds[$val['member_id']] = $val['member_id'];

            }

            if(!empty($memberIds)){

                $memberModel = new MemberModel();

                $members =  $memberModel->getMemberInfoByMemberId($memberIds);

            }

            foreach ($balance_rank_data as $k => $list){

                $balance_rank_data[$k]['member']=$members[$list['member_id']] ?? [];

                $balance_rank_data[$k]['rank']=$k+1;

            }

        }



        //会员积分排行榜

        $integral_time_type=$this->request->param('integral_time_type/d',1);

        $member_integral_logs_model = new MemberIntegralLogsModel();

        if($integral_time_type == 1) $member_integral_logs_model = $member_integral_logs_model->whereMonth('add_time');

        if($integral_time_type == 2) $member_integral_logs_model = $member_integral_logs_model->whereYear('add_time');

        $integral_rank_data = $member_integral_logs_model->where([

            ['shop_id','=',$this->request->shopId],

            ['type','=',1],

        ])->field('member_id,sum(integral) as integral')

            ->group('member_id')->order(['integral'=>'desc'])

            ->limit(5)->select()->toArray();

        if(!empty($integral_rank_data)){

            $memberIds =  [];

            foreach($integral_rank_data as $val){

                $memberIds[$val['member_id']] = $val['member_id'];

            }

            if(!empty($memberIds)){

                $memberModel = new MemberModel();

                $members =  $memberModel->getMemberInfoByMemberId($memberIds);

            }

            foreach ($integral_rank_data as $k => $list){

                $integral_rank_data[$k]['member']=$members[$list['member_id']] ?? [];

                $integral_rank_data[$k]['rank']=$k+1;

            }

        }



        $return=[

            'shop'=>$shop,

            'kefu'=>$kefu,

            'balance_recharge_data'=>$balance_recharge_data,

            'balance_consume_data'=>$balance_consume_data,

            'coupon_consume_data'=>$coupon_consume_data,

            'balance_rank_data'=>$balance_rank_data,

            'integral_rank_data'=>$integral_rank_data

        ];

        return $this->success($return);

    }

}

